//
//  PYPresentCustomView.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/5/16.
//

import UIKit

enum PresentStyle {
    
    case Sheet
    case Alert
}

protocol PYPresentLifeCircle : UIView{
    
    func viewDidShow()
    
    func viewDidHide()
}
protocol PYPresentCustomView : PYPresentLifeCircle{
    
    //是否已经展示
    var isShowed : Bool {get}
    
    /// 展示弹框的控制器
    var showViewController : BasePresentViewController? {get}
    
    /// 弹框样式
    /// - Returns: ~
    func styleForCustomContentView() -> PresentStyle
    
    /// 弹框大小
    /// - Returns: ～
    func sizeForCustomContentView() -> CGSize
    
    /// 蒙版颜色
    /// - Returns: ~
    func maskBackgroundColor() -> UIColor
    
    /// 展示
    /// - Parameter viewController: 展示的控制器
    func show(to viewController:UIViewController)
    
    /// 隐藏
    func hide(dismissCompletion:EmptyCompleteBlock?)
    
    /// 是否点击背景后隐藏
    func isTouchedToHide() -> Bool
}

var KEY_POPUP_PROPERTY = "keyForPopupProperty"

class PYPresentProperty : NSObject{
    
    var showViewController : BasePresentViewController?
    
    var isShowed : Bool = false
}

extension PYPresentCustomView{
    
    var isShowed : Bool{
        get{
            return self.propertys.isShowed
        }
    }
    var showViewController : BasePresentViewController?{
        get{
            return self.propertys.showViewController
        }
    }
    var propertys : PYPresentProperty{
        get{
            var _propertys = objc_getAssociatedObject(self, &KEY_POPUP_PROPERTY) as? PYPresentProperty
            if _propertys == nil {
                _propertys = PYPresentProperty()
                objc_setAssociatedObject(self, &KEY_POPUP_PROPERTY, _propertys, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            }
            return _propertys!
        }
        set{
            objc_setAssociatedObject(self, &KEY_POPUP_PROPERTY, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    func show(to viewController:UIViewController){
        
        self.propertys.showViewController = BasePresentViewController()
        self.propertys.showViewController?.show(contentView:self, to: viewController)
    }
    
    func hide(dismissCompletion:EmptyCompleteBlock? = nil){
    
        self.propertys.showViewController?.hide(dismissCompletion: dismissCompletion)
    }
    
    func maskBackgroundColor() -> UIColor{
        return "0x000000".HexColor(alpha: 0.4)
    }
    
    func isTouchedToHide() -> Bool{
        return true
    }
    
    func viewDidShow(){}
    
    func viewDidHide(){}
}

class BasePresentViewController: UIViewController {
    
    weak var customPresentContentView : PYPresentCustomView?
    
    /// 刷新需要推出的视图
    func reloadPresentView(contentView:PYPresentCustomView){
        
        self.view.backgroundColor = contentView.maskBackgroundColor()
        self.view.addSubview(contentView)
        let style = contentView.styleForCustomContentView()
        let superFrame = self.view.frame
        let contentSize = contentView.sizeForCustomContentView()
        
        //x轴始终居中显示
        let contentX : Double = (superFrame.width - contentSize.width) / 2.0
        //y轴根据样式变动
        var contentY : Double = 0.0
        if style == . Alert{
            contentY = (superFrame.height - contentSize.height) / 2.0
        }
        else{
            contentY = superFrame.height - contentSize.height
        }
        contentView.frame = CGRect.init(x: contentX, y: contentY, width: contentSize.width, height: contentSize.height)
    }
    
    /// 展示到控制器中
    /// - Parameter viewController: 需要展示的控制器
    func show(contentView:PYPresentCustomView, to viewController:UIViewController){
        
        self.reloadPresentView(contentView: contentView)
        self.customPresentContentView = contentView
        self.customPresentContentView?.propertys.isShowed = true
        self.modalPresentationStyle = .custom
        self.modalTransitionStyle = .crossDissolve
        self.customPresentContentView?.viewDidShow()
        viewController.present(self, animated: true, completion: nil)
    }
    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        super.dismiss(animated: flag, completion: completion)
        
        self.customPresentContentView?.propertys.isShowed = false
        self.customPresentContentView?.viewDidHide()
        self.customPresentContentView?.removeFromSuperview()
    }
    /// 关闭当前控制器
    func hide(dismissCompletion:EmptyCompleteBlock? = nil){
        
        self.dismiss(animated: true,completion: dismissCompletion)
    }
    
    // 通过查看点击事件，判断是否需要关闭当前页面。
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        guard let firstTouch = touches.first else {
            return
        }
        if let contentView = self.customPresentContentView{
            if !contentView.isTouchedToHide(){
                return
            }
            let customLocation = firstTouch.location(in: self.view)
            let customFrame = contentView.frame
            
            let isIn = customFrame.contains(customLocation)
            guard !isIn else {
                return
            }
        }
        self.hide()
    }
}
